/**
 * Created by yaohuan on 2018/03/23.
 */
define(function () {
    // 全局遮罩
    var mask = mui.createMask();
    var self;
    function WdbjPlay() {
        self = this;
        self.mData = {
           // option:[],//选择客户和服务单号
            orderList: [],
            form:{
                //MMRT012: '',//单号
                //MMRT013: '',//客户
                MMRT007: '',//需求日期
                MMRT009: '',
                MMRT010: '',
                MMRT011: ''
            },
            index : null
        }
        self.mData.orderList = app.pageParameters['wdbj-play'];
        self.init();
    }


    WdbjPlay.prototype.initVue = function(){
            new Vue({
            el: document.getElementById("apply"),
            data: function () {
                return self.mData.form;
            }
            ,
            methods: {
                submit: function () {
                    self.reback_application();
                },
                cancel: function () {
                    mask.close();
                    $(".modal1").css("display", "none");
                },
            }
        })
    }

    //备件申请 编辑
    WdbjPlay.prototype.spare_application = function () {
        var i = self.mData.index;
        self.mData.form.MMRT007 = self.mData.orderList[i].MMRT007;
        self.mData.form.MMRT009 = self.mData.orderList[i].MMRT009;
        self.mData.form.MMRT010 = self.mData.orderList[i].MMRT010;
        self.mData.form.MMRT011 = self.mData.orderList[i].MMRT011;
        //self.mData.form.MMRT012 = self.mData.orderList[i].MMRT012;
        //self.mData.form.MMRT013 = self.mData.orderList[i].MMRT013;
        $(".modal1").css("display", "block");
        mask.show();
    }
    //编辑回显
    WdbjPlay.prototype.reback_application = function () {
        var i = self.mData.index;
        self.mData.orderList[i].MMRT007 = self.mData.form.MMRT007;
        self.mData.orderList[i].MMRT009 = self.mData.form.MMRT009;
        self.mData.orderList[i].MMRT010 = self.mData.form.MMRT010;
        self.mData.orderList[i].MMRT011 = self.mData.form.MMRT011;
        //self.mData.orderList[i].MMRT012 = self.mData.form.MMRT012;
        //self.mData.orderList[i].MMRT013 = self.mData.form.MMRT013;
        Vue.set(self.mData.orderList,i,self.mData.orderList[i]);
        $(".modal1").css("display", "none");
        mask.close();
    }

    WdbjPlay.prototype.sure = function () {


        var _xdata = {};
        var list = [];
        for(var i=0;i<self.mData.orderList.length;i++){
            if(self.mData.orderList[i].checked == true){
                list.push(self.mData.orderList[i]);
            }
        }
        _xdata.list = JSON.stringify(list);
        app.ajax({
            xurl: "api/wx/wdbj/savePjsq",
            // xtime: '1d',
            xdata: _xdata,
            cb: function (result) {
                if (result.ok) {
                    app.toast('备件申请成功');
                    for(var i=0,flag=true;i<self.mData.orderList.length;flag ? i++ : i){
                        if(self.mData.orderList[i].checked == true){
                            self.mData.orderList.splice(i,1);
                            flag = false;
                        }else{
                            flag = true;
                        }
                    }
                    app.back(2);
                }else{
                    app.toast(result.data.message);
                }
            }
        });
    }


    WdbjPlay.prototype.init = function () {
        mui.ready(function () {
            self.initVue();
            new Vue({
                el: document.getElementById('playInfo'),
                data: function () {
                    return {
                        model: self.mData.orderList
                    }
                },
                methods: {
                    edit: function (index) {
                        self.mData.index = index;
                        self.spare_application(index);
                    }
                },
                created: function(){
                    setTimeout(function(){
                        mui('.mui-numbox').numbox();
                    },200);
                }
            });

            $('#sq_sure').on('tap', function () {
                //检验
                var list1 = self.mData.orderList;
                for (var a = 0; a < list1.length; a++) {
                    if (list1[a].MMRT007 == '' || list1[a].MMRT007 == null) {
                        app.toast('NO.' + (a + 1) + '需求日期为空，请检查');
                        return;
                    } else if (list1[a].MMRT009 == '' || list1[a].MMRT009 == null) {
                        app.toast('NO.' + (a + 1) + '收件人为空，请检查');
                        return;
                    }else if (list1[a].MMRT010 == '' || list1[a].MMRT010 == null) {
                        app.toast('NO.' + (a + 1) + '电话为空，请检查');
                        return;
                    }else if (list1[a].MMRT011 == '' || list1[a].MMRT011 == null) {
                        app.toast('NO.' + (a + 1) + '配送地址为空，请检查');
                        return;
                    }
                }
                app.confirm({
                    msg: '是否提交？',
                    ok: function (e) {
                        self.sure();
                    }
                })
            });

        });
    };

    return new WdbjPlay();
});